<?php

namespace App\Http\Controllers;

use App\Models\PhonicsShare;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class AdminPhonicsShareController extends Controller
{
    public function retrieveStudentHistory(Request $request, $id)
    {
        $request->merge(['id' => $id]);
        $this->validate($request, [
            'id' => 'required|integer|min:1'
        ]);

        $studentId = intval($id);
        $shares = PhonicsShare::where('student_id', $studentId)->get();
        $result = [];
        foreach ($shares as $share) {
            $result[] = [
                'openGId' => $share->getOpenGid(),
                'bonus' => $share->getBonus(),
                'createdAt' => $share->getCreatedAt()->timestamp
            ];
        }
        return new JsonResponse($result);
    }
}
